package ihm;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import questions.Question;
import questions.QuestionPicker;

@SuppressWarnings("serial")
public class MTITesterFrame extends JFrame implements ActionListener, KeyListener {

	private JPanel contentPane;
	private JLabel jlQuestion;
	private JLabel jlMessage;
	private JButton jbValidate;
	private QuestionPicker qPicker;
	private JTextField jtfAnswer;
	private JLabel lblScore;
	private JLabel lblRemainingQuestions;
	private Question currentQuestion;

	/**
	 * Create the frame.
	 */
	public MTITesterFrame(QuestionPicker qPicker) {
		this.qPicker = qPicker;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 641, 439);
		
		this.contentPane = new JPanel();
		this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(this.contentPane);
		this.contentPane.setLayout(null);
		
		this.lblScore = new JLabel("Score: 0");
		this.lblScore.setBounds(10, 11, 65, 14);
		this.contentPane.add(this.lblScore);
		
		this.currentQuestion = this.qPicker.getNewQuestion();
		this.jlQuestion = new JLabel(currentQuestion.getQuestion());
		this.jlQuestion.setHorizontalAlignment(SwingConstants.CENTER);
		this.jlQuestion.setFont(new Font("Verdana", Font.PLAIN, 15));
		this.jlQuestion.setBounds(56, 79, 512, 66);
		this.contentPane.add(jlQuestion);
		
		this.lblRemainingQuestions = new JLabel("Remaining questions: "+qPicker.getRemainingQuestionsNumber());
		this.lblRemainingQuestions.setBounds(10, 36, 139, 14);
		this.contentPane.add(this.lblRemainingQuestions);
		
		this.jtfAnswer = new JTextField();
		this.jtfAnswer.setBounds(56, 159, 512, 20);
		this.jtfAnswer.setColumns(10);
		this.jtfAnswer.addKeyListener(this);
		this.contentPane.add(this.jtfAnswer);
		
		this.jbValidate = new JButton("Validate");
		this.jbValidate.setBounds(270, 190, 89, 23);
		this.contentPane.add(this.jbValidate);
		this.jbValidate.addActionListener(this);
		
		this.jlMessage = new JLabel("");
		this.jlMessage.setBounds(56, 230, 512, 23);
		this.jlMessage.setForeground(Color.RED);
		this.jlMessage.setHorizontalAlignment(SwingConstants.CENTER);
		this.contentPane.add(jlMessage);
		
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		this.jlMessage.setText("");
		
		String answer = this.jtfAnswer.getText().trim();
		if (answer.equals("")) {
			this.jlMessage.setForeground(Color.RED);
			this.jlMessage.setText("Empty answer");
			return;
		}
		String correctAnswer = this.qPicker.answer(answer);
		
		if (answer.equals(correctAnswer)) {
			this.jlMessage.setForeground(Color.GREEN);
			this.jlMessage.setText("Correct!");
		} else {
			this.jlMessage.setForeground(Color.RED);
			this.jlMessage.setText("Wrong! The answer was "+correctAnswer);
		}
		
		this.currentQuestion = this.qPicker.getNewQuestion();
		if (currentQuestion == null) end();
		updatePanel();
		this.jtfAnswer.requestFocus();
	}

	private void end() {
		long elapsedTime = qPicker.getElapsedTime()/1000000000;
		long minutes = (elapsedTime%3600)/60;
		long secs = (elapsedTime%3600)%60;
		JOptionPane.showMessageDialog(this, "End of the game!\nFinal score: "+qPicker.getQuestionsCorrect()+"/"+qPicker.getQuestionsCount()+"\nElapsed time: "+ (minutes<10 ? "0" : "")+minutes+" minute(s) and "+(secs<10 ? "0" : "")+secs+ " seconds", "End of the game", JOptionPane.INFORMATION_MESSAGE);
		System.exit(0);
	}

	private void updatePanel() {
		this.lblScore.setText(qPicker.getQuestionsCorrect()+"/"+(qPicker.getQuestionsCount()-1));
		this.lblRemainingQuestions.setText(qPicker.getRemainingQuestionsNumber()+"");
		this.jlQuestion.setText(currentQuestion.getQuestion());
		this.jtfAnswer.setText("");
	}

	@Override
	public void keyPressed(KeyEvent ke) {
		if (ke.getSource().equals(this.jtfAnswer)) {
			if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
				this.jbValidate.doClick();
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
	}
}

